function addToCart(item){
	var query = "item_id="+item+"&function=add";
    sendData(query,"add");
}

function deleteFromCart(item){
	var query = "item_id="+item+"&function=delete";
	sendData(query,"delete");
}

function updateCart(item){
	var query = "item_id="+item+"&function=delete";
	sendData(query,"update");
}

function changeQuantity(obj,gold){
	var item = obj.getAttribute("name");
	var qty = obj.value;
	var query = "item_id="+item+"&qty="+qty+"&function=update";
	sendData(query,gold);
}

// send query to the server to retrieve items
function sendData(query,arg){
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			if(arg == "add" || arg == "delete"){
				$("table#simple_sc_table").replaceWith(modify_simple_sc());
			}else{
				$("table#shopping_cart_table").replaceWith(modify_shopping_cart(arg));
			}
		}
	}
	xmlhttp.open("GET","retrieveItem.php?"+query,true);
	xmlhttp.send();
}

function modify_simple_sc(){
	var total,price,price_total = eval(0);
	var xmldoc,x,xx,i,txt;
	txt = "<table id=\"simple_sc_table\"><tr>";
	txt = txt + "<th class=\"simple_sc_item\">Item</th>";
	txt = txt + "<th class=\"simple_sc_price\">price</th><th></th></tr>";
	
	xmldoc = xmlhttp.responseXML;
	//alert("test2.1" + xmldoc); // test
	x = xmldoc.getElementsByTagName("total");
	//alert("test2.2"); // test
	total = x[0].firstChild.nodeValue;
	//alert("test2.3"); // test
	
	// if result contains more than one items
	if(total > 0){
		// construct a table
		x = xmldoc.getElementsByTagName("item");
		for(i=0;i<x.length;i++){
			txt = txt + "<tr><td class=\"simple_sc_item\">";
			xx = x[i].getElementsByTagName("name");
			txt = txt + xx[0].firstChild.nodeValue;
			txt = txt + "</td><td class=\"simple_sc_price\">"
			xx = x[i].getElementsByTagName("price_buy");
			price = xx[0].firstChild.nodeValue;
			txt = txt + price;
			price_total = price_total + eval(price);
			txt = txt + "</td><td>";
			txt = txt + "<input type=\"image\" src=\"../../images/shop/ico_remove.gif\" onclick=\"deleteFromCart(";
			xx = x[i].getElementsByTagName("id");
			txt = txt + xx[0].firstChild.nodeValue;
			txt = txt + ")\"></td></tr>";
		}
		price_total = "<td id=\"simple_sc_total_price\" class=\"simple_sc_price\">" + price_total + "</td>";
		$("#simple_sc_total_price").replaceWith(function(){
			return price_total;
		});
	}else{
		$("#simple_sc_total_price").replaceWith("<td id=\"simple_sc_total_price\" class=\"simple_sc_price\">-</td>");
	}
	txt = txt + "</table>";
	//alert("test3" + txt) // test
	return txt;
}

// construct the table of shopping cart
function modify_shopping_cart(gold){
	var total,id,price,price_total = eval(0);
	var xmldoc,x,xx,i,txt;
	txt = "<table id=\"shopping_cart_table\">";
	txt = txt + "<thead><tr><th width=10%>&nbsp&nbsp&nbsp&nbsp</th>";
	txt = txt + "<th width=20%>NAME</th><th width=30%>DESCRIPTION</th>";
	txt = txt + "<th width=5%>QTY</th><th width=10%>UNIT PRICE</th>";
	txt = txt + "<th width=10%>SUBTOTAL</th><th width=5%></th></tr></thead>";
	xmldoc = xmlhttp.responseXML;
	
	x = xmldoc.getElementsByTagName("total");
	total = x[0].firstChild.nodeValue;
	
	// if result contains more than one items
	if(total > 0){
		// construct a table
		x = xmldoc.getElementsByTagName("item");
		subtotal = eval(0);
		for(i=0;i<x.length;i++){
			txt = txt + "<tr><td><img src=\"";
			xx = x[i].getElementsByTagName("image");
			txt = txt + xx[0].firstChild.nodeValue;
			txt = txt + "\"></td><td>";
			xx = x[i].getElementsByTagName("name");
			txt = txt + xx[0].firstChild.nodeValue;
			txt = txt + "</td><td>"
			xx = x[i].getElementsByTagName("effects");
			txt = txt + xx[0].firstChild.nodeValue;
			txt = txt + "</td><td><input type=text class=\"quantity\" name=\"";
			xx = x[i].getElementsByTagName("id");
			id = xx[0].firstChild.nodeValue;
			txt = txt + id;
			txt = txt +	"\" value=\"";
			xx = x[i].getElementsByTagName("quantity");
			quantity = xx[0].firstChild.nodeValue;
			txt = txt + quantity;
			//txt = txt + "\" size =\"2\"/></td><td>"; // test
			txt = txt + "\" size =\"2\" onchange=\"changeQuantity(this," + gold + ")\"/></td><td>";
			xx = x[i].getElementsByTagName("price_buy");
			price = xx[0].firstChild.nodeValue;
			txt = txt + price;
			txt = txt + "</td><td>";
			subtotal = eval(quantity) * eval(price);
			txt = txt + subtotal;
			price_total = price_total + eval(subtotal);
			txt = txt + "</td><td><input type=\"image\" src=\"../../images/shop/ico_remove.gif\" onclick=\"updateCart(";
			txt = txt + id;
			txt = txt + ")\"/></td></tr>";
		}
		
		var price_total_div = "<div id=\"total_price\">" + price_total + "</div>";
		$("div#total_price").replaceWith(function(){
			return price_total_div;
		});
		var price_total_hidden = "<input id=\"total_price_hidden\" type=\"hidden\" name=\"total\" value=\"" + price_total + "\"/>";
		$("input#total_price_hidden").replaceWith(function(){
			return price_total_hidden;
		});
		var temp = eval(gold) - price_total;
		var change = "<div id=\"total_price_change\">" + temp + "</div>";
		$("div#total_price_change").replaceWith(function(){
			return change;
		});
		
		if(eval(gold) < price_total){
			$(".gold_change").css("color","red");
			var img = "<input type=\"image\" id=\"checkout_button\" name=\"checkout\" disabled src=\"../../images/shop/bt_checkout.gif\" >";
			$("input#checkout_button").replaceWith(function(){
				return img;
			});
		}else{
			$(".gold_change").css("color","purple");
			var img = "<input type=\"image\" id=\"checkout_button\" name=\"checkout\" src=\"../../images/shop/bt_checkout.gif\" >";
			$("input#checkout_button").replaceWith(function(){
				return img;
			});
		}
	}else{
		$("div#total_price").replaceWith("<div id=\"total_price\" class =\"shopping_cart_total\">-</div>");
		var tmp = "<p>Shopping cart is empty.</p>";
		txt = tmp + txt;
		var img = "<input type=\"image\" id=\"checkout_button\" name=\"checkout\" disabled src=\"../../images/shop/bt_checkout.gif\" >";
		$("input#checkout_button").replaceWith(function(){
			return img;
		});
		$("div#total_price_change").replaceWith("<div id=\"total_price_change\">-</div>");
	}
	txt = txt + "</table>";
	//alert(txt); // test
	return txt;
}